from django.db import connection

from app.decorator import jwt_auth
from libs.common import success
from libs.db import DB
from libs.form import RawJsonForm


@jwt_auth
def banner(request, auth):
    shop_id = auth.get('shop_id')
    form = RawJsonForm(request.body)
    typ = form.get_int('type', 2)
    filters = {'type': typ, 'is_show': 1, 'shop_id': shop_id}
    is_video = form.get_int('is_video', None)
    if is_video is not None:
        filters.update({'is_video': is_video})
    with connection.cursor() as cursor:
        dh = DB('main_banner', conn=cursor)
        rows = dh.find('*', filters)
    return success(rows)
